package com.zhizuobiao.coura.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhizuobiao.coura.stock.HAStockList;
import com.zhizuobiao.coura.stockhelper.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by coura on 2016/12/13.
 */

public class HAStockListAdapter extends BaseAdapter {

    private Context context;
    private List<HAStockList.ResultBean.DataBean> list;

    public HAStockListAdapter(Context context,List<HAStockList.ResultBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.stock_listview, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvName.setText(list.get(position).getCname());
        holder.tvSymbol.setText(list.get(position).getSymbol());
        holder.tvLasttrade.setText(list.get(position).getPrice());
        double chg = Double.parseDouble(list.get(position).getChg());
        if(chg>0){
            holder.tvChangepercent.setTextColor(context.getResources().getColor(R.color.colorRed));
        }else{
            holder.tvChangepercent.setTextColor(context.getResources().getColor(R.color.colorGreen));
        }
        holder.tvChangepercent.setText(chg+"%");
        return convertView;
    }

   /* static class ViewHolder {
        private TextView name;
        private TextView symbol;
        private TextView lasttrade;
        private TextView changepercent;
    }*/

    class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_symbol)
        TextView tvSymbol;
        @BindView(R.id.tv_lasttrade)
        TextView tvLasttrade;
        @BindView(R.id.tv_changepercent)
        TextView tvChangepercent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
